package com.avic.rong.activity.mine;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.RelativeLayout;

import com.avic.rong.BaseActivity;
import com.avic.rong.R;
import com.avic.rong.adapter.HelpCenterAdapter;
import com.avic.rong.api.HttpPostService;
import com.avic.rong.bean.mine.HelpCenterBean;
import com.avic.rong.utils.ToastTools;
import com.example.minato.minastore.utils.AppUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import io.reactivex.Observable;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;

public class UserHelperCenterActivity extends BaseActivity {

    //0是关于融融网   1是帮助中心
    public static String ARG_URL = "arg_url";

    //标题  网址
    public ArrayList<HelpCenterBean.ChannelListBean.ContentBean> contentBeans = new ArrayList<>();





    @BindView(R.id.recv_helpcenter)
    RecyclerView recyclerView;
    private HelpCenterAdapter adapter;

    @Override
    protected int getContentViewId() {
        return R.layout.activity_helper_centerl;
    }

    @Override
    protected void initView() {
        tvTitle.setText("帮助中心");
        ivRight.setBackgroundResource(R.mipmap.slh);


        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        recyclerView.setLayoutManager(linearLayoutManager);

        adapter = new HelpCenterAdapter(contentBeans, this);
        recyclerView.setAdapter(adapter);


        initData();

    }

    private void initData() {
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(AppUtils.BASE_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                .build();

        HttpPostService httpPostService = retrofit.create(HttpPostService.class);

        Observable<HelpCenterBean> observable = httpPostService.helpCenter();

        observable.subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<HelpCenterBean>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(HelpCenterBean helpCenterBeans) {
                        if (helpCenterBeans!=null) {
                            HelpCenterBean.ChannelListBean channelListBean = helpCenterBeans.getChannelList().get(1);
                            adapter.setChannelName(channelListBean.getChannelName());
                            if (channelListBean!=null) {
                                List<HelpCenterBean.ChannelListBean.ContentBean> content = channelListBean.getContent();
                                if (content!=null&&content.size()>0) {
                                    for (HelpCenterBean.ChannelListBean.ContentBean bean : content) {
                                        if (bean.getSubheadTitle() != null && bean.getTitle() != null) {
                                            contentBeans.add(bean);
                                            adapter.notifyDataSetChanged();
                                        }
                                    }
                                }
                            }
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        ToastTools.showPublicToast(e.getMessage());
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

    @Override
    protected void addListener() {

    }


    public static Intent queryIntent(Context context, String url) {
        Intent intent = new Intent(context, UserHelperCenterActivity.class);
        intent.putExtra(ARG_URL, url);
        return intent;
    }

}
